/**
 * 
 */
package rentel.logbook.car;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 * Car form validator methods 
 * @author Jiri Styblo
 *
 */
public class CarFormValidator implements Validator {

	private enum ErrorMessages {
		EMPTY_NAME("Name is empty!"),
		NON_UNIQUE_NAME("Car name isn't unique.");
		
		private final String value;
		
		private ErrorMessages(String value) {
			this.value = value;
		}
		
		public String getValue() {
			return value;
		}
	}
	
	/* (non-Javadoc)
	 * @see org.springframework.validation.Validator#supports(java.lang.Class)
	 */
	public boolean supports(Class<?> clazz) {
		return Car.class.equals(clazz);
	}

	/* (non-Javadoc)
	 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
	 */
	public void validate(Object target, Errors errors) {
		// TODO check that name is unique in DB
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "noCode", ErrorMessages.EMPTY_NAME.getValue());
	}

}
